package com.enjoyor.soa.traffic.rest.efoms.dubbo.invoke;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.enjoyor.soa.traffic.api.dto.idcs.VehiclePassDto;
import com.enjoyor.soa.traffic.rest.efoms.common.Global;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Component
public class DubboSolrClientInvoke {

	@Value("${solr.client.url}")
	private String solrClientUrl;	
	
	private static DubboSolrClientInvoke instance;

	public static DubboSolrClientInvoke getInstance() {
		if (instance == null) {
			instance = new DubboSolrClientInvoke();
			//instance.initUrl();
			//instance.initType();
		}
		return instance;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultPojo getPageVehSear(Page page, String bayonetId, String startTime, String endTime, 
			String token) throws Exception {
		ResultPojo resultPojo = new ResultPojo();
		StringBuilder path = new StringBuilder(solrClientUrl).append("?token=").append(token);
		if (page == null) {
			path.append("&pageSize=10&currentPage=1");
		} else {
			path.append("&pageSize=").append(page.getPageSize()).append("&currentPage=").append(page.getCurrentPage());
		}
		if (bayonetId != null && !bayonetId.isEmpty()) {
			path.append("&pointIds=").append(URLEncoder.encode(bayonetId, "utf-8"));
		}
		if (startTime != null && !startTime.isEmpty()) {
			path.append("&startTime=").append(URLEncoder.encode(startTime, "utf-8"));
		}
		if (endTime != null && !endTime.isEmpty()) {
			path.append("&endTime=").append(URLEncoder.encode(endTime, "utf-8"));
		}
		
		URL restServiceURL = new URL(path.toString());

		HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
		httpConnection.setRequestMethod("GET");
		httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

		if (httpConnection.getResponseCode() != 200) {
			throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
		}

		BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "UTF-8"));
		StringBuffer sb = new StringBuffer();
		String line = "";
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		ResultPojo result = JSONObject.parseObject(sb.toString(), ResultPojo.class);
		if (result.getAppCode().equals("0")) {
			List<JSONObject> list = (List<JSONObject>) ((JSONObject) result.getResultList()).get("result");
			((JSONObject) result.getResultList()).remove("result");
			Page<VehiclePassDto> p = JSONObject.parseObject(((JSONObject) result.getResultList()).toJSONString(),
					Page.class);
			List<VehiclePassDto> listDto = new ArrayList<VehiclePassDto>();
			for (JSONObject obj : list) {
				VehiclePassDto passDto = new VehiclePassDto();
				passDto.setBayonet_id(obj.getString("point_id"));//卡口编号
				//passDto.setBayonet_name(obj.getString("bayonet_name"));
				passDto.setId(obj.getString("id"));//ID
				passDto.setLane_id(obj.getString("lane_number"));//车道；
				if (obj.getString("speed_limit_max") != null && !obj.getString("speed_limit_max").equals(""))
					passDto.setMax_speed(Integer.parseInt(obj.getString("speed_limit_max")));//最大限速；
				if (obj.getString("speed_limit_min") != null && !obj.getString("speed_limit_min").equals(""))
					passDto.setMin_speed(Integer.parseInt(obj.getString("speed_limit_min")));//最低限速；
				if (obj.getString("speed") != null && !obj.getString("speed").equals(""))
					passDto.setSpeed(Integer.parseInt(obj.getString("speed")));//速度；
				passDto.setPass_id(obj.getString("id"));
				passDto.setPass_time(obj.getString("passing_time").replace("T", " ").replace("Z", ""));//过车时间；
				String pic_directory = obj.getString("pic_directory");
				String url = obj.getString("veh_pic");
				String pic_url1 = pic_directory+url;
				passDto.setPic_url1(obj.getString("pic_url1"));//图片路径；
				//passDto.setPic_url2(obj.getString("pic_url2"));
				//passDto.setPic_url3(obj.getString("pic_url3"));
				passDto.setVehicle_color(obj.getString("veh_color"));//车辆颜色；
				passDto.setVehicle_color_name(Global.DicTraffic.get(obj.getString("veh_color")));//车辆颜色名称；
				passDto.setVehicle_plate(obj.getString("license_number"));//车牌号码；
				passDto.setVehicle_plate_type(obj.getString("sr_license_type"));//车牌类型；
				passDto.setVehicle_plate_type_name(Global.DicTraffic.get(obj.getString("sr_license_type")));//车牌类型名称；
				passDto.setVehicle_plate_color(obj.getString("sr_license_color"));//车牌颜色；
				passDto.setVehicle_plate_color_name(Global.DicTraffic.get(obj.getString("sr_license_color")));//车牌颜色；
				passDto.setDirection(obj.getString("direction"));
				passDto.setDir_name(Global.DicTraffic.get(obj.getString("direction")));
				listDto.add(passDto);
			}
			//p.setResult(listDto);
			//result.setResultList(p);
			resultPojo = ResultHelper.getResult(listDto);
		}
		return resultPojo;
	}
}
